<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <div class="public_main">
    <div class="query">
      <el-form inline ref="queryFormRef" :model="queryParams" label-width="120px" size="small">
        <el-form-item label="上传日期:">
          <el-date-picker
            v-model="dateRangeCreateTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
          ></el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="机型:">
          <el-select v-model="queryParams.modelNumber" placeholder="请选择机型">
            <el-option v-for="item in modelNumberList" :label="item.name" :value="item.mark" :key="item.mark"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="批次:">
          <el-select v-model="queryParams.batchNumber" placeholder="请选择机型">
            <el-option v-for="item in modelNumberList" :label="item.name" :value="item.mark" :key="item.mark"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="机型：">
            <el-select v-model="queryParams.modelNumber" clearable placeholder="请选择机型" @change="modelChange">
              <el-option v-for="item in modelNumberList" :label="item.name" :value="item.mark" :key="item.mark"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="批次号：">
            <el-select v-model="queryParams.batchNumber" clearable placeholder="请选择批次号">
              <el-option v-for="(item, index) in batchList" :label="batchList[index]" :value="batchList[index]" :key="index"></el-option>
            </el-select>
          </el-form-item>
        <el-form-item label="数据质量检测状态:">
          <el-select v-model="queryParams.qualityCheck">
            <el-option label="通过" :value="0"></el-option>
            <el-option label="不通过" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="query_btns">
          <el-button size="small" type="primary" icon="search" @click="doQuery">查询</el-button>
          <el-button size="small" type="default" icon="refresh" @click="doReset">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="return_btn">
        <el-tooltip content="返回"><img :src="back" alt="" @click="goBack" /></el-tooltip>
      </div>
    </div>
    <div class="table_div">
      <el-button size="small" type="primary" class="upload_btn" plain icon="Plus" @click="handleVisible('import')">模型上传</el-button>
      <!-- <el-button size="small" type="primary" class="upload_btn" plain icon="Plus" @click="handleVisible('check')">数据质量检测</el-button> -->
      <el-table :data="tableData" v-loading="loading" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="上传日期" align="center" prop="uploadTimeShow" fixed="left"> </el-table-column>
        <el-table-column label="压缩包名称" align="center" prop="fileName"> </el-table-column>
        <el-table-column label="文件大小" align="center" prop="fileSizeWithUnit"> </el-table-column>
        <el-table-column label="所属机型" align="center" prop="modelNumber"> </el-table-column>
        <el-table-column label="所属批次" align="center" prop="batchNumber"> </el-table-column>
        <!-- <el-table-column label="上传人" align="center" prop="remarks"> </el-table-column> -->
        <el-table-column label="数据质量检测状态" align="center" prop="qualityCheck">
          <template #default="scope">
            <span>{{ scope.row.qualityCheck === 0 ? '通过' : '不通过' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="250">
          <template #default="scope">
            <el-button size="small" link type="primary" icon="View" @click="viewAll(scope.row)">查看全部文件</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagenation">
        <el-pagination
          v-show="total > 0"
          layout="prev, pager, next,jumper"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <AllFileView 
      v-if="dialogVisible"
      :dialogVisible="dialogVisible"
      :id="fileId"
      @handleEvent="handleEvent"
    />
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import back from '@/assets/images/back.svg';
import { getCollectList, getMeshModelStructureList, getBatchNo } from '@/api/dataAcquisition';
import AllFileView from './AllFileView.vue';
const emits = defineEmits(['handleEvent']);
const props = defineProps(['tableName']);
const { proxy } = getCurrentInstance();
const tableData = ref([]);
const dialogVisible = ref(false);
const fileId = ref(null); 
const ids = ref([]);
const addVisible = ref(false);
const modelList = ref([]);
const preViewVisible = ref(false);
const preViewParams = ref();
const dateRangeCreateTime = ref(['', '']);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  modelNumber: '',
  batchNumber: '',
  qualityCheck: undefined,
  uploadTimeStartTime: '',
  uploadTimeEndTime: '',
  orderByColumn: 'createTime',
  isAsc: 'descending'
});
const modelNumberList = ref([]);
const batchList = ref([]);
const total = ref(0);
const uploadOp2Visible = ref(false);
const currentRow = ref(null);
const resultList = ref([]);
const loading = ref(false);

const handleCurrentChange = (number) => {
  queryParams.value.pageNum = number;
  doQuery();
};
const handleView = () => {
  preViewVisible.value = false;
};
const viewAll = (row) => {
  fileId.value = row.id;
  dialogVisible.value = true;
}
const goBack = () => {  
  emits('handleEvent', { component: 'Main'});
}
//查询数据列表
const doQuery = () => {
  loading.value = true;
  tableData.value = [];
  queryParams.value.uploadTimeStartTime = dateRangeCreateTime.value[0];
  queryParams.value.uploadTimeEndTime = dateRangeCreateTime.value[1];
  getCollectList(queryParams.value)
    .then((res) => {
      // 处理数据
      loading.value = false;
      total.value = res?.total;
      tableData.value = res.rows;
    })
    .catch(() => {
      loading.value = false;
    }).finally(() => {
      loading.value = false;
    });
};
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
};
// 回调函数
const handleEvent = (data) => {
  if (data?.type === 'refresh') {
    doQuery();
  } else if (data?.type === 'cancel') {
    dialogVisible.value = false;
  } 
};
const doReset = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    modelNumber: '',
    batchNumber: '',
    qualityCheck: undefined,
    uploadTimeStartTime: '',
    uploadTimeEndTime: '',
    orderByColumn: 'createTime',
    isAsc: 'descending'
  };
  batchList.value = [];
  dateRangeCreateTime.value = ['', ''];
  doQuery();
};
// 打开弹窗方法
const handleVisible = (param, data) => {
  if (param == 'import') {
    emits('handleEvent', { component: 'Import', tableName: props.tableName });
  }
};

//删除
const handleDelete = (row) => {
  const dataIds = row?.id ? [row?.id] : ids.value;
  // deleteBoundaryOptionValidate(dataIds).then(async res => {
  //   let infoMsg = !res.data ? res.msg : (row?.id ? '是否确认删除当前数据项？' : '是否确认删除当前所选全部数据项？')
  //   ElMessageBox.confirm(infoMsg, '提示', {
  //     confirmButtonText: '确定',
  //     cancelButtonText: '取消',
  //     type: 'warning'
  //   }).then(() => {
  //     deleteBoundaryOption(dataIds)
  //     .then((res) => {
  //       if (res) {
  //         ElMessage({ showClose: true, message: '删除成功。', type: 'success' });
  //         doQuery();
  //       }
  //     })
  //     .catch(() => {
  //       ElMessage({ showClose: true, message: '删除失败。', type: 'error' });
  //     });
  //   });
  // })
};
// 获取机型
const getModelList = () => {
  getMeshModelStructureList({parentId: 0, pageSize: 999}).then(res => {
    if (res) {
      modelNumberList.value = res.rows;
    }
  })
}

// 获取批次号
const modelChange = (val) => {
  if (val) {
    queryParams.value.batchNumber = '';
    getBatchNo({modelNumber: val}).then(res => {
      if (res) {    
        batchList.value = res.data;
      }
    })
  }
}
onMounted(() => {
  doQuery();
  getModelList();
});
</script>
<style lang="scss" scoped>
.public_main {
  .query {
    position: relative;
    border: 1px solid #dddddd7a;
    padding: 20px 10px 0 10px;
    .query_btns {
      padding-left: 100px;
    }
    .return_btn {
      position: absolute;
      top: 30px;
      right: 30px;
      // width: 100%;
      > img {
        height: 45px;
        width: 45px;
        margin-left: 10px;
        cursor: pointer;
      }
    }
  }
  .table_div {
    margin-top: 10px;
    border: 1px solid #dddddd7a;
    padding-bottom: 10px;
    .upload_btn {
      margin: 10px 10px;
    }
    .pagenation {
      display: flex;
      justify-content: end;
      margin-top: 10px;
    }
  }
  :deep(.el-form-item__content) {
    width: 220px;
  }
  :deep(.el-tree-node__label) {
    font-size: 12px;
    color: #606266;
  }
}
</style>
      